package com.zdj.aoplibrary.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;

/**
 * 判断权限管理模块
 * Created by zdj on 16-7-5.
 */

public class SecurityCheckManager {

    private Context mContext;

    private SecurityCheckManager(Context context) {
        mContext = context;
    }

    private static SecurityCheckManager gSecurityCheckManager;

    public static SecurityCheckManager createInstance(Context context) {
        if (gSecurityCheckManager == null) {
            gSecurityCheckManager = new SecurityCheckManager(context);
        }
        return gSecurityCheckManager;

    }

    public static SecurityCheckManager getInstance() {
        return gSecurityCheckManager;

    }

    public boolean checkPermission(String permissionName) {
        PackageManager pkm = mContext.getPackageManager();
        // 适配android M 运行时权限检查
        if (pkm.checkPermission(permissionName, mContext.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
            // 申请RECORD_AUDIO权限
            ActivityCompat.requestPermissions((AppCompatActivity) mContext, new String[]{permissionName}, PermissionsConstants.RECORD_AUDIO_REQUEST_CODE);
            return  false;
        }else {
            return true;
        }
    }
}
